package com.wf.smartcomm.server.admin.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.bean.MerchantUser;
import com.wf.smartcomm.server.bean.Terminal;
import com.wf.smartcomm.server.model.PushMessage;
import com.wf.smartcomm.server.push.Pusher;
import com.wf.smartcomm.server.service.MerchantService;
import com.wf.smartcomm.server.service.MerchantUserService;
import com.wf.smartcomm.server.service.TerminalService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.viewmodel.MerchantCustom;

@Controller
@Scope("prototype")
public class MerchantAction extends BaseAction implements ModelDriven<MerchantCustom>{

	private MerchantCustom merchantCustom = new MerchantCustom();
	
	@Resource
	private MerchantService merchantService;
	
	@Resource
	private MerchantUserService merchantUserService;
	
	@Resource
	private TerminalService terminalService;

	public void save()throws Exception{
		merchantCustom.setObjectId(IdUtils.uuid());
		merchantCustom.setCreatedAt(new Date());
		merchantCustom.setCreatedBy(currentUser);
		merchantCustom.setUpdatedAt(merchantCustom.getCreatedAt());
		merchantCustom.setUpdatedBy(currentUser);
		Merchant merchant = new Merchant();
		PropertyUtils.copyProperties(merchant, merchantCustom);
		MerchantUser mUser =merchantUserService.getByUsername(merchantCustom.getmUser().getUsername());
		if (mUser == null) {
			merchantService.save(merchant);
			mUser = merchantCustom.getmUser();
			mUser.setObjectId(IdUtils.uuid());
			mUser.setCreatedBy(currentUser);
			mUser.setCreatedAt(new Date());
			mUser.setUpdatedAt(mUser.getCreatedAt());
			mUser.setMerchant(merchant);
			mUser.setScale(merchantCustom.getScale());
			mUser.setUpdatedBy(currentUser);
			merchantUserService.save(mUser);
		}
		printJsonData(200);
	}
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		//sSearch = jo.getString("sSearch");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		int  initEcho = Integer.parseInt(sEcho)+1;
		merchantService.list(page);
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void editScale()throws Exception{
		BigDecimal scale = merchantCustom.getScale();
		Merchant merchant = merchantService.get(merchantCustom.getObjectId());
		merchant.setScale(scale);
		merchantService.update(merchant);
		MerchantUser merchantUser = merchantUserService.get(merchant);
		merchantUser.setScale(scale);
		merchantUserService.update(merchantUser);
		List<Terminal> terminals = terminalService.list(merchant);
		PushMessage msg = new PushMessage();
		msg.setPushType(ConstantUtils.PUSH_TYPE_SCALE_MODIFY);
		Pusher pusher = PushUtils.getClientPusher();
		for (Terminal terminal : terminals) {
			terminal.setScale(scale);
			terminalService.update(terminal);
			pusher.push0x20Message(terminal.getPushId().getBytes(), outGson.toJson(msg).getBytes());
		}
		pusher.close();
		printJsonData(200);
	}

	@Override
	public MerchantCustom getModel() {
		// TODO Auto-generated method stub
		return merchantCustom;
	}
}
